| 1 |  |  | import execa from 'execa' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import {chmodSync, existsSync, mkdirSync, unlinkSync, writeFileSync} from 'fs' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import OS from '../client/OS' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import limitMaxFilesPlist from '../templates/limitMaxFilesPlist' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import myCnf from '../templates/myCnf' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import Service from './service' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | abstract class Mysql extends Service { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     requireRoot = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     isEndOfLife = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     abstract versionName: string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     configRootPath = `${OS.getInstance().usrLocalDir}/etc` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     configPath = `${this.configRootPath}/my.cnf` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     maxFilesConfPath = '/Library/LaunchDaemons/limit.maxfiles.plist' // TODO: This is Mac only, make it cross platform. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     mysqlDirectoryPath = `${OS.getInstance().usrLocalDir}/var/mysql` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     rootPassword = 'root' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     systemDatabases = ['sys', 'performance_schema', 'information_schema'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     configure = async (): Promise<boolean> => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         await this.removeConfiguration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // await this.setMaxFilesConfig() // TODO: Fix permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         await this.linkDatabase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         await this.installConfiguration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         await this.setRootPassword() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     removeConfiguration = async (): Promise<void> => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (existsSync(this.configPath)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             await unlinkSync(this.configPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (existsSync(`${this.configPath}.default`)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             await unlinkSync(`${this.configPath}.default`) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     installConfiguration = async (): Promise<void> => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         await chmodSync(this.mysqlDirectoryPath, 0o777) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (!existsSync(this.configRootPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             await mkdirSync(this.configRootPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         let config = myCnf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (this.service === 'mariadb' || this.service === '[email protected]') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             config = config.replace('show_compatibility_56=ON', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (this.service === '[email protected]') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             config = config.replace('query_cache_size=67108864', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             config = config.replace('query_cache_type=1', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             config = config.replace('query_cache_limit=4194304', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             config = config.replace('query_cache_size=67108864', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         await writeFileSync(this.configPath, config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     setMaxFilesConfig = async (): Promise<void> => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         await writeFileSync(this.maxFilesConfPath, limitMaxFilesPlist) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         await execa('launchctl', ['load', '-w', this.maxFilesConfPath]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     linkDatabase = async (): Promise<void> => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         await OS.getInstance().serviceCtl.link(this.service) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     // TODO: We should get the current password from the Jale config instead. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     setRootPassword = async (oldPassword = '', password = 'root'): Promise<boolean> => { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             await execa('mysqladmin', ['-u', 'root', `--password='${oldPassword}'`, 'password', password], { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 stdio: 'inherit' | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             }) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             return true | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } catch (e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             // Password probably is not equal to oldPassword so it failed. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             return false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | export default Mysql | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |